package net.dgg.zz.production.singleRejectedBack.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.tmd.foundation.platform.datapermission.entity.DataAuthDTO;
import net.dgg.tmd.foundation.platform.datapermission.service.DataPermissionAssocService;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.base.service.BaseService;
import net.dgg.zz.common.constant.ScNoteType;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.entity.CustomerRecord;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.systemRecord.entity.SystemRecord;
import net.dgg.zz.common.systemRecord.service.SystemRecordService;
import net.dgg.zz.common.utils.MQMessageSendUtil;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.production.chargeback.dao.ZzAuditOrderDao;
import net.dgg.zz.production.chargeback.dao.ZzProductOrderDao;
import net.dgg.zz.production.entity.ProductOrder;
import net.dgg.zz.production.inProcess.service.ZzInProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ZzChargeBackRejectedService extends BaseService {

    @Autowired
    private ZzAuditOrderDao auditOrderDao;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private ZzProductOrderDao zzProductOrderDao;
    @Autowired
    private ZzInProcessService zzInProcessService;
    @Autowired
    private SystemRecordService systemRecordService;

    @Autowired
    protected DataPermissionAssocService dataPermissionAssocService;

    @Autowired
    private CommonService commonService;

    @Transactional(readOnly = true)
    public List<Map> queryListPage(Map map) {
        DataAuthDTO dataAuthDTO = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds("zzsc_sc_charge_back");
        map.put("dataAuthDTO", dataAuthDTO);
        map.put("userAlias", "t");
        map.put("userColumn", "flow_user_id");
        map.put("deptAlias", "t");
        map.put("deptColumn", "flow_user_org_id");
        Integer count = this.auditOrderDao.count(map);
        map.put("count", count);
        if (count <= 0) {
            return new ArrayList<Map>(0);
        }
        if (null == map.get("orderColumn")) {
            map.put("orderDir", "desc");
            map.put("orderColumn", "updateTime");
        }
        List<Map> listPage = auditOrderDao.queryMap(map);
        map.put("count", count);
        return listPage;
    }

    /**
     * 退单恢复办理
     *
     * @param id 孙上峰
     */
    @Transactional
    public void restoreOrder(Long id) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        Assert.notNull(userEntity, "未获取到登陆人，请重新登录！");
        Assert.notNull(id, "生产订单ID不能为空！");
        ProductOrder scProductOrder = zzInProcessService.getScProductOrderById(id);
        if(StringUtils.isEmpty(scProductOrder.getCurrentNodeId())){
            scProductOrder.setCurrentNodeId(scProductOrder.getLatestEndNodeId());
            scProductOrder.setCurrentNodeId(scProductOrder.getLatestEndNodeName());
        }
        Assert.notNull(scProductOrder, "未查询到该生产订单的信息！");
        if (null != scProductOrder.getBeforeStatus() && !"".equals(scProductOrder.getBeforeStatus())) {
            scProductOrder.setStatus(scProductOrder.getBeforeStatus()); //恢复到退单前的状态
        } else {
            scProductOrder.setStatus(ScProductConstant.SC_ORDER_IN_PROCESS); //办理中
        }
        this.updateOpt(scProductOrder, userEntity);
        zzProductOrderDao.update(scProductOrder);
        //-------操作记录------------
        SystemRecord systemRecord = new SystemRecord();
        systemRecord.setId(DggKeyWorker.nextId());
        systemRecord.setWorkId(scProductOrder.getId());
        systemRecord.setWorkTableName("zzsc_product_order");
        systemRecord.setRemarks("恢复办理");
        systemRecord.setSource("2");//来源生产
        systemRecord.setOperateType("44");//操作类型 : 恢复办理
        systemRecordService.save(systemRecord);
        //-----------操作记录------------------
        String msgContent="您好，您的客户"+scProductOrder.getCustomerName()+"订单"+scProductOrder.getOrderNo()+"办理的"
                +scProductOrder.getScProductOrderNo()+"业务，订单已恢复办理";
        commonService.sendQgjMessage(scProductOrder.getOrderId(),scProductOrder.getBusinessTypeCode(),userEntity.getId(),userEntity.getRealName(),userEntity.getLoginName(),msgContent);
        //通过MQ推送消息到UC
        CustomerRecord customerRecord = new CustomerRecord();
        customerRecord.setCustomerId(scProductOrder.getCustomerId());
        customerRecord.setTableName("zzsc_product_order");
        customerRecord.setTableId(scProductOrder.getId());
        customerRecord.setTableNo(scProductOrder.getScProductOrderNo());
        customerRecord.setRemarkType(ScNoteType.ZZSC_NOTE_CODE6);
        customerRecord.setCreaterId(userEntity.getId());
        customerRecord.setContent("退单恢复办理");
        MQMessageSendUtil.ucMessageSend(customerRecord);

        //提交之后，将订单信息同步到企大顺生产中心
        MQMessageSendUtil.qdsMessageSend(scProductOrder, null,  userEntity);
    }
}
